//
//  DPCommonTool.m
//  polyv_video_plugin
//
//  Created by Jared on 2019/11/6.
//

#import "DPCommonTool.h"

@implementation DPCommonTool
+(instancetype)sharedTool {
    static dispatch_once_t onceToken;
    static DPCommonTool *shareTool = nil;
    dispatch_once(&onceToken, ^{
        shareTool = [[DPCommonTool alloc]init];
        shareTool.selfBuyDic = [[NSDictionary alloc]init];
        shareTool.sysVoiceValue = 0.3;
        shareTool.learnY = 0;
    });
    return shareTool;
}

+ (CGSize)getStrSizeWithString:(NSString *)str font:(UIFont *)font constraitSize:(CGSize)conSize {
//    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
////    paragraphStyle.lineSpacing = 0;
//    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
//    NSDictionary * attributes = @{
//                                  NSFontAttributeName:font,
//                                  NSParagraphStyleAttributeName: paragraphStyle
//                                  };
//
//    CGSize strSize = [str boundingRectWithSize:conSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
    
    
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [paragraphStyle setLineSpacing:0.0f];
    [paragraphStyle setAlignment:NSTextAlignmentJustified];
    if (font==nil) {
        return CGSizeZero;
    }
    NSDictionary *attribute = @{
                                NSParagraphStyleAttributeName: paragraphStyle,
                                NSFontAttributeName: font};
    
    CGSize  labelsize3 = [str boundingRectWithSize:conSize
                                             options:
                          NSStringDrawingTruncatesLastVisibleLine |
                          NSStringDrawingUsesLineFragmentOrigin
                                          attributes:attribute context:nil].size;
    return  labelsize3;
}

+ (CGSize)getStrSizeWithString:(NSString *)str font:(UIFont *)font constraitSize:(CGSize)conSize lineSpace:(CGFloat)lp {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    if (lp==0) {
        // paragraphStyle.lineSpacing = 0;
    }else{
    paragraphStyle.lineSpacing = lp;
    }
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    
    NSDictionary * attributes = @{
                                  NSFontAttributeName:font,
                                  NSParagraphStyleAttributeName: paragraphStyle
                                  };

    CGSize strSize = [str boundingRectWithSize:conSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
    return  strSize;
}

+ (UIImage *)imageWithColor:(UIColor *)color{
    // 一个像素
    CGRect rect = CGRectMake(0, 0, 1, 1);
    // 开启上下文
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

+ (BOOL)isEmptyForString:(NSString *)str {
    if (!str||![str isKindOfClass:[NSString class]]) {
        return YES;
    } else {
        //A character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085).
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        
        //Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        
        if ([trimedString length] == 0) {
            return YES;
        } else {
            if ([trimedString containsString:@"null"]||[trimedString containsString:@"NULL"]) {
                return YES;
            }
            return NO;
        }
    }
}

#pragma mark  屏幕亮度-----
- (void)setScreenBrightness:(CGFloat)brtValue{
    [[UIScreen mainScreen] setBrightness: brtValue];
}
- (CGFloat)getScreenBrightnessValue{
    CGFloat currentLight = [[UIScreen mainScreen] brightness];
    return currentLight;
}

@end
